/*     */ package com.newcrm.webapi.sub;
/*     */ 
/*     */ import au.com.bytecode.opencsv.CSVReader;
/*     */ import com.newcrm.bean.CompanyInfo;
/*     */ import com.newcrm.bean.ExtInfo;
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.config.Config;
/*     */ import com.newcrm.config.Const;
/*     */ import com.newcrm.config.ProvinceReader;
/*     */ import com.newcrm.db.ConnectionManager;
/*     */ import com.newcrm.db.CustomerCategoryManager;
/*     */ import com.newcrm.db.CustomerManager;
/*     */ import com.newcrm.db.ImportManager;
/*     */ import com.newcrm.exception.CustomerIsExistException;
/*     */ import com.newcrm.exception.DBException;
/*     */ import com.newcrm.exception.ImportFileIsNull;
/*     */ import com.newcrm.exception.NotFoundException;
/*     */ import com.newcrm.thread.LuceneThread;
/*     */ import com.newcrm.util.ExcelUtil;
/*     */ import com.newcrm.util.Util;
/*     */ import com.newcrm.util.XMLUtil;
/*     */ import com.newcrm.webapi.AbstractAction;
/*     */ import com.newcrm.webapi.AbstractHttpServlet;
/*     */ import java.io.File;
/*     */ import java.io.FileReader;
/*     */ import java.io.IOException;
/*     */ import java.io.PrintWriter;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.ArrayList;
/*     */ import java.util.Enumeration;
/*     */ import java.util.HashMap;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import javax.servlet.ServletException;
/*     */ import javax.servlet.http.HttpServletRequest;
/*     */ import javax.servlet.http.HttpServletResponse;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class CustomerImportSave extends AbstractAction
/*     */ {
/*  53 */   private static Logger logger = Logger.getLogger(CustomerImportSave.class
/*  54 */     .getName());
/*     */ 
/*  56 */   private CompanyInfo company = null;
/*  57 */   private String userId = null;
/*     */ 
/*  59 */   private String companyName = null;
/*  60 */   private String peopleName = null;
/*  61 */   private String title = null;
/*  62 */   private String companyNote = null;
/*  63 */   private int categoryId = 0;
/*  64 */   private List<ExtInfo> peopleExtInfos = new ArrayList();
/*  65 */   private List<ExtInfo> companyExtInfos = new ArrayList();
/*     */ 
/*  67 */   private String existCustomerIds = "";
/*  68 */   int successNum = 0;
/*  69 */   int existNum = 0;
/*  70 */   int errorNum = 0;
/*     */ 
/*  72 */   private Map<String, String> mapping = new HashMap();
/*  73 */   private int importId = 0;
/*  74 */   private String path = null;
/*  75 */   private String indexPath = null;
/*     */ 
/*     */   public CustomerImportSave(AbstractHttpServlet servlet, String path, String indexPath)
/*     */   {
/*  79 */     super(servlet);
/*  80 */     this.path = path;
/*  81 */     this.indexPath = indexPath;
/*     */   }
/*     */ 
/*     */   public void parse(HttpServletRequest request, HttpServletResponse response)
/*     */     throws IOException, ServletException, NotFoundException
/*     */   {
/*  87 */     response.setContentType("text/html;charset=utf-8");
/*     */ 
/*  89 */     PrintWriter out = response.getWriter();
/*  90 */     out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\"> ");
/*  91 */     out.println("<html xmlns=\"http://www.w3.org/1999/xhtml\"> ");
/*  92 */     out.println("<head>");
/*  93 */     out.println("  <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" /> ");
/*  94 */     out.println("  <meta http-equiv=\"X-UA-Compatible\" content=\"IE=Edge\" />");
/*     */ 
/*  96 */     User user = super.getLoginUser(request);
/*  97 */     this.userId = user.userId;
/*  98 */     this.company = super.getCompany(user.companyId);
/*  99 */     String fileType = 
/* 100 */       Util.getURLParameter(request.getParameter("fileType"));
/* 101 */     String fileName = 
/* 102 */       Util.getURLParameter(request.getParameter("fileName"));
/* 103 */     ImportManager importManager = new ImportManager();
/*     */     try {
/* 105 */       this.importId = importManager.add(fileType, fileName, 
/* 106 */         user.companyId, user.userId);
/*     */     } catch (DBException e) {
/* 108 */       logger.warn("DBException", e);
/*     */     } catch (SQLException e) {
/* 110 */       logger.warn("SQLException", e);
/*     */     }
/*     */ 
/* 113 */     if (this.importId != 0)
/*     */     {
/* 116 */       getMapping(request);
/*     */       try {
/* 118 */         readImportFile(request, out);
/*     */       }
/*     */       catch (ImportFileIsNull localImportFileIsNull)
/*     */       {
/*     */       }
/*     */ 
/* 127 */       out.println("<script>parent.finish(" + this.importId + "," + this.successNum + 
/* 128 */         "," + this.existNum + "," + this.errorNum + ",'" + 
/* 129 */         this.existCustomerIds + "'); </script>");
/* 130 */       out.flush();
/*     */ 
/* 133 */       new LuceneThread(this.indexPath, false).start();
/*     */     }
/*     */   }
/*     */ 
/*     */   private void getMapping(HttpServletRequest request)
/*     */   {
/* 147 */     Enumeration paramNames = request.getParameterNames();
/* 148 */     while (paramNames.hasMoreElements()) {
/* 149 */       String paramName = (String)paramNames.nextElement();
/* 150 */       String paramValue = Util.getURLParameter(request
/* 151 */         .getParameter(paramName));
/* 152 */       if (paramName.contains("mapping_"))
/*     */       {
/* 155 */         if ((paramValue != null) && (paramValue.trim().length() != 0))
/*     */         {
/* 158 */           String[] array = paramName.split("_");
/* 159 */           if (array.length == 2)
/*     */           {
/* 162 */             this.mapping.put(array[1], paramValue);
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void readImportFile(HttpServletRequest request, PrintWriter out)
/*     */     throws ImportFileIsNull
/*     */   {
/* 175 */     User user = super.getLoginUser(request);
/* 176 */     String filePath = 
/* 177 */       Util.getURLParameter(request.getParameter("filePath"));
/* 178 */     String fileType = 
/* 179 */       Util.getURLParameter(request.getParameter("fileType"));
/* 180 */     int visible = Util.getURLIntParameter(request.getParameter("visible"));
/* 181 */     String ignoreHeader = Util.getURLParameter(request
/* 182 */       .getParameter("ignoreHeader"));
/*     */ 
/* 184 */     String permissionsId = Util.getURLParameter(request
/* 185 */       .getParameter("permissionsId"));
/* 186 */     if (permissionsId == null) {
/* 187 */       permissionsId = "";
/*     */     }
/*     */ 
/* 190 */     String[] arrayPermissionsIds = permissionsId.split(",");
/* 191 */     int permissionsIdIdx = 0;
/*     */ 
/* 196 */     File file = new File(this.path, filePath);
/* 197 */     List result = null;
/* 198 */     CSVReader csvReader = null;
/*     */     try
/*     */     {
/* 201 */       if ("excel".equals(fileType)) {
/* 202 */         result = ExcelUtil.readExcel(file);
/* 203 */       } else if ("csv".equals(fileType)) {
/* 204 */         csvReader = new CSVReader(new FileReader(file));
/* 205 */         result = csvReader.readAll();
/* 206 */       } else if ("xml".equals(fileType)) {
/* 207 */         result = XMLUtil.readXML(file);
/*     */       }
/*     */ 
/* 213 */       if ((result == null) || (result.size() == 0)) {
/* 214 */         throw new ImportFileIsNull();
/*     */       }
/*     */ 
/* 217 */       int rowIndex = 0;
/*     */ 
/* 220 */       Connection conn = ConnectionManager.getInstance().getConnection();
/* 221 */       if (conn != null) {
/*     */         try {
/* 223 */           for (Object itemO : result)
/*     */           {
                    String[] item = (String[])itemO;
/* 226 */             rowIndex++;
/* 227 */             if ((!"1".equals(ignoreHeader)) || 
/* 228 */               (rowIndex != 1))
/*     */             {
/* 232 */               if ((item != null) && (item.length != 0))
/*     */               {
/* 236 */                 readRow(item);
/*     */ 
/* 239 */                 if (((this.companyName != null) && (this.companyName.trim().length() != 0)) || (
/* 240 */                   (this.peopleName != null) && 
/* 241 */                   (this.peopleName.trim()
/* 241 */                   .length() != 0)))
/*     */                 {
/* 246 */                   CustomerManager manager = new CustomerManager();
/*     */                   try {
/* 248 */                     if (permissionsIdIdx >= arrayPermissionsIds.length) {
/* 249 */                       permissionsIdIdx = 0;
/*     */                     }
/* 251 */                     int[] customerIds = manager.importCustomer(conn, 
/* 252 */                       this.companyName, this.categoryId, this.peopleName, this.title, 
/* 253 */                       this.companyNote, this.companyExtInfos, 
/* 254 */                       this.peopleExtInfos, user.userId, 
/* 255 */                       user.companyId, visible, 
/* 256 */                       arrayPermissionsIds[permissionsIdIdx], 
/* 257 */                       false, this.importId);
/* 258 */                     if (customerIds == null) {
/* 259 */                       this.errorNum += 1;
/*     */                     } else {
/* 261 */                       this.successNum += 1;
/*     */ 
/* 263 */                       permissionsIdIdx++;
/*     */                     }
/*     */                   } catch (DBException e) {
/* 266 */                     this.errorNum += 1;
/*     */                   }
/*     */                   catch (CustomerIsExistException e) {
/* 269 */                     if (this.existCustomerIds.length() == 0) {
/* 270 */                       this.existCustomerIds = String.valueOf(e
/* 271 */                         .getCustomerId());
/* 272 */                       this.existNum += 1;
/*     */                     }
/* 275 */                     else if (!this.existCustomerIds.contains(
/* 275 */                       String.valueOf(e.getCustomerId()))) {
/* 276 */                       this.existCustomerIds = 
/* 278 */                         (this.existCustomerIds + 
/* 277 */                         "," + 
/* 278 */                         String.valueOf(e.getCustomerId()));
/* 279 */                       this.existNum += 1;
/*     */                     }
/*     */                   }
/*     */ 
/* 283 */                   out.println("<script>parent.changeProgressbar(" + 
/* 284 */                     rowIndex + "); </script>");
/* 285 */                   out.flush();
/*     */                 }
/*     */               }
/*     */             }
/*     */           }
/*     */         } finally { try { DbUtils.close(conn);
/*     */           } catch (SQLException e) {
/* 292 */             logger.warn("close conn failed", e);
/*     */           }
/*     */         }
/*     */         try
/*     */         {
/* 290 */           DbUtils.close(conn);
/*     */         } catch (SQLException e) {
/* 292 */           logger.warn("close conn failed", e);
/*     */         }
/*     */       }
/*     */     }
/*     */     catch (Exception ex) {
/* 297 */       logger.warn("read import file failed", ex);
/* 298 */       throw new ImportFileIsNull();
/*     */     } finally {
/* 300 */       if (csvReader != null)
/*     */         try {
/* 302 */           csvReader.close();
/*     */         } catch (IOException e) {
/* 304 */           logger.warn("close csv reader failed", e);
/*     */         }
/*     */     }
/*     */   }
/*     */ 
/*     */   private void readRow(String[] item)
/*     */   {
/* 319 */     init();
/*     */ 
/* 321 */     int idx = -1;
/* 322 */     for (String data : item)
/*     */     {
/* 324 */       idx++;
/*     */ 
/* 326 */       String mappingValue = (String)this.mapping.get(String.valueOf(idx));
/* 327 */       if ((mappingValue != null) && (mappingValue.trim().length() != 0))
/*     */       {
/* 330 */         if ((data != null) && (data.trim().length() != 0))
/*     */         {
/* 335 */           if ("companyName".equals(mappingValue)) {
/* 336 */             this.companyName = data;
/*     */           }
/* 341 */           else if ("category".equals(mappingValue)) {
/* 342 */             this.categoryId = getCustomerCategoryId(data);
/*     */           }
/* 347 */           else if ("name".equals(mappingValue)) {
/* 348 */             this.peopleName = data;
/*     */           }
/* 352 */           else if ("title".equals(mappingValue)) {
/* 353 */             this.title = data;
/*     */           }
/* 357 */           else if ("background".equals(mappingValue)) {
/* 358 */             this.companyNote = data;
/*     */           }
/* 363 */           else if (mappingValue.contains("field")) {
/* 364 */             String[] tmpArray = mappingValue.split("_");
/* 365 */             if (tmpArray.length == 3)
/*     */             {
/* 368 */               ExtInfo extInfo = new ExtInfo();
/* 369 */               extInfo.type = Util.str2Int(tmpArray[1]);
/* 370 */               extInfo.info1 = data;
/* 371 */               int fieldType = Util.str2Int(tmpArray[2]);
/* 372 */               if (fieldType == 2)
/* 373 */                 this.peopleExtInfos.add(extInfo);
/*     */               else {
/* 375 */                 this.companyExtInfos.add(extInfo);
/*     */               }
/*     */             }
/*     */ 
/*     */           }
/*     */           else
/*     */           {
/* 382 */             String[] tmpArray = mappingValue.split("_");
/* 383 */             if (tmpArray.length == 3)
/*     */             {
/* 388 */               String[] info2Array = null;
/* 389 */               boolean isPhone = false;
/* 390 */               if ("phone".equals(tmpArray[0])) {
/* 391 */                 isPhone = true;
/* 392 */                 info2Array = Const.ARRAY_CUSTOMER_EXT_PHONE_INFO2;
/*     */               } else {
/* 394 */                 info2Array = Const.ARRAY_CUSTOMER_EXT_INFO2;
/*     */               }
/*     */ 
/* 398 */               if ("im".equals(tmpArray[0])) {
/* 399 */                 ExtInfo extInfo = new ExtInfo();
/*     */ 
/* 401 */                 extInfo.type = Util.str2Int(tmpArray[1]);
/* 402 */                 extInfo.info1 = data;
/* 403 */                 extInfo.info2 = Const.ARRAY_CUSTOMER_EXT_IM_INFO2[0];
/* 404 */                 extInfo.info3 = Util.getArrayValue(Config.getInstance().imType, 
/* 405 */                   Util.str2Int(tmpArray[2]));
/* 406 */                 this.peopleExtInfos.add(extInfo);
/*     */               }
/* 411 */               else if ("website".equals(tmpArray[0]))
/*     */               {
/* 413 */                 String[] websites = data.split("\\|");
/* 414 */                 for (String website : websites)
/* 415 */                   if ((website != null) && (website.trim().length() != 0))
/*     */                   {
/* 419 */                     if (website.toLowerCase().startsWith("http://")) {
/* 420 */                       website = website.substring(7);
/*     */                     }
/* 422 */                     ExtInfo extInfo = new ExtInfo();
/* 423 */                     extInfo.type = Util.str2Int(tmpArray[1]);
/* 424 */                     extInfo.info1 = website;
/* 425 */                     extInfo.info2 = Util.getArrayValue(info2Array, 
/* 426 */                       Util.str2Int(tmpArray[2]));
/* 427 */                     this.companyExtInfos.add(extInfo);
/*     */                   }
/*     */               }
/*     */               else
/*     */               {
/* 432 */                 ExtInfo extInfo = new ExtInfo();
/* 433 */                 extInfo.type = Util.str2Int(tmpArray[1]);
/* 434 */                 extInfo.info1 = data;
/* 435 */                 extInfo.info2 = Util.getArrayValue(info2Array, 
/* 436 */                   Util.str2Int(tmpArray[2]));
/*     */ 
/* 439 */                 if ("address".equals(tmpArray[0]))
/*     */                 {
/* 441 */                   ProvinceReader reader = ProvinceReader.getInstance();
/* 442 */                   String[] citys = reader.getProvinceInfo(data);
/* 443 */                   if (citys != null) {
/* 444 */                     extInfo.info3 = citys[0];
/* 445 */                     extInfo.info4 = citys[1];
/*     */                   }
/* 447 */                   this.companyExtInfos.add(extInfo);
/*     */                 }
/*     */                 else {
/* 450 */                   this.peopleExtInfos.add(extInfo);
/*     */                 }
/*     */ 
/* 456 */                 if ((isPhone) && (
/* 457 */                   (Util.str2Int(tmpArray[2]) == 1) || 
/* 458 */                   (Util.str2Int(tmpArray[2]) == 3)))
/* 459 */                   this.companyExtInfos.add(extInfo);
/*     */               }
/*     */             }
/*     */           }
/*     */         }
/*     */       }
/*     */     }
/* 466 */     if ((this.peopleName == null) || (this.peopleName.trim().length() == 0))
/* 467 */       for (ExtInfo info : this.peopleExtInfos)
/* 468 */         if ((info.type == 1) || 
/* 469 */           (info.type == 2))
/*     */         {
/* 471 */           if (!isInCompanyExtInfo(info))
/*     */           {
/* 473 */             this.companyExtInfos.add(info);
/*     */           }
/*     */         }
/*     */   }
/*     */ 
/*     */   private boolean isInCompanyExtInfo(ExtInfo info)
/*     */   {
/* 488 */     if (this.companyExtInfos.size() == 0) {
/* 489 */       return false;
/*     */     }
/*     */ 
/* 492 */     for (ExtInfo info2 : this.companyExtInfos)
/*     */     {
/* 494 */       if (info2.type == info.type)
/*     */       {
/* 498 */         if ((info2.info1.equals(info.info1)) && 
/* 499 */           (info2.info2.equals(info.info2))) {
/* 500 */           return true;
/*     */         }
/*     */       }
/*     */     }
/* 504 */     return false;
/*     */   }
/*     */ 
/*     */   private int getCustomerCategoryId(String name)
/*     */   {
/* 514 */     if ((name == null) || (name.trim().length() == 0)) {
/* 515 */       return 0;
/*     */     }
/* 517 */     if (this.company.customerCategorys != null) {
/* 518 */       for (Map category : this.company.customerCategorys) {
/* 519 */         if (name.equals(category.get("CategoryName"))) {
/* 520 */           return ((Integer)category.get("CategoryId")).intValue();
/*     */         }
/*     */       }
/*     */     }
/*     */ 
/* 525 */     CustomerCategoryManager manager = new CustomerCategoryManager();
/*     */     try {
/* 527 */       int categoryId = manager.add(this.company.id, name, 
/* 528 */         1, this.userId);
/* 529 */       this.company.customerCategorys = manager.list(this.company.id, 
/* 530 */         1);
/* 531 */       return categoryId;
/*     */     } catch (DBException e) {
/* 533 */       logger.warn("add category or list category failed", e);
/*     */     } catch (SQLException e) {
/* 535 */       logger.warn("add category or list category failed", e);
/*     */     }
/* 537 */     return 0;
/*     */   }
/*     */ 
/*     */   private void init() {
/* 541 */     this.companyName = null;
/* 542 */     this.peopleName = null;
/* 543 */     this.title = null;
/* 544 */     this.companyNote = null;
/* 545 */     this.categoryId = 0;
/* 546 */     this.peopleExtInfos.clear();
/* 547 */     this.companyExtInfos.clear();
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.webapi.sub.CustomerImportSave
 * JD-Core Version:    0.6.1
 */